﻿using BookingSystemModel;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace WcfBookingService.DataLayer
{
    public class SubscriptionManager
    {
        private DbHandler dbHandler = null;

        public SubscriptionManager(DbHandler _dbh)
        {
            this.dbHandler = _dbh;
        }

        public int CreateSubscription(Subscription subscription)//String _title, String _description, int _attendeeAmount, DateTime _start, DateTime _end, Entity _payee, String _note)
        {
            int rowId = -1;
            try
            {
                dbHandler.Open();
                dbHandler.AddCommand(@"INSERT INTO dbo.Subscription 
                                       VALUES(@Title, @Description, @AttendeeAmount, @IsActive, @IsDeleted, @ValidFrom, @ValidTo, @PayeeEntityId, @PayeeEntityTypeCode, @Note);");
                dbHandler.AddParameter("Title", subscription.Title);
                dbHandler.AddParameter("Description", subscription.Description);
                dbHandler.AddParameter("AttendeeAmount", subscription.AttendeeAmount);
                dbHandler.AddParameter("IsActive", subscription.IsActive);
                dbHandler.AddParameter("IsDeleted", subscription.IsDeleted);
                dbHandler.AddParameter("ValidFrom", subscription.ValidFrom);
                dbHandler.AddParameter("ValidTo", subscription.ValidTo);
                dbHandler.AddParameter("PayeeEntityId", subscription.Payee.Id);
                dbHandler.AddParameter("PayeeEntityTypeCode", subscription.Payee.TypeCode);
                dbHandler.AddParameter("Note", subscription.Note);
                rowId = dbHandler.Command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                dbHandler.Close();
            }
            return rowId;
        }
        /*
        public Subscription GetSubscription(int id)
        {
            Subscription subscription = null;
            try
            {
                dbHandler.Open();
                dbHandler.AddCommand("SELECT * FROM dbo.Subscription WHERE Number = @Number");
                dbHandler.AddParameter("Id", id);
                SqlDataReader reader = dbHandler.Command.ExecuteReader();
                while (reader != null && reader.Read())
                {
                    subscription = new Subscription(DbHandler.ParseValue<Int32>(reader, "id"),
                                        DbHandler.ParseValue<Boolean>(reader, "IsPrimary"),
                                        DbHandler.ParseValue<Int32>(reader, "Extension"),
                                        DbHandler.ParseValue<Int32>(reader, "CountryCode"),
                                        DbHandler.ParseValue<Int32>(reader, "AreaCode"),
                                        DbHandler.ParseValue<Int32>(reader, "Number"));
                }
                reader.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            finally
            {
                dbHandler.Close();
            }
            return subscription;
        }

        public List<Phone> GetAllSubscriptions()
        {
            List<Phone> phoneList = new List<Phone>();
            try
            {
                dbHandler.Open();
                dbHandler.AddCommand("SELECT * FROM dbo.Phone");
                SqlDataReader reader = dbHandler.Command.ExecuteReader();
                while (reader != null && reader.Read())
                {
                    phoneList.Add(new Phone(DbHandler.ParseValue<Int32>(reader, "id"),
                                            DbHandler.ParseValue<Boolean>(reader, "IsPrimary"),
                                            DbHandler.ParseValue<Int32>(reader, "Extension"),
                                            DbHandler.ParseValue<Int32>(reader, "CountryCode"),
                                            DbHandler.ParseValue<Int32>(reader, "AreaCode"),
                                            DbHandler.ParseValue<Int32>(reader, "Number")));
                }
                reader.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            finally
            {
                dbHandler.Close();
            }
            return phoneList;
        }

        public Boolean UpdateSubscription(Phone phone)
        {
            Boolean isSuccess = false;
            try
            {
                dbHandler.Open();
                dbHandler.AddCommand(@"UPDATE dbo.Phone
                                        SET IsPrimary = @IsPrimary,
                                            Extension = @Extension,
                                            CountryCode = @CountryCode,
                                            AreaCode = @AreaCode,
                                            Number = @Number
                                        WHERE id = @Id");
                dbHandler.AddParameter("IsPrimary", phone.IsPrimary);
                dbHandler.AddParameter("Extension", phone.Extension);
                dbHandler.AddParameter("CountryCode", phone.CountryCode);
                dbHandler.AddParameter("AreaCode", phone.AreaCode);
                dbHandler.AddParameter("Number", phone.Number);
                dbHandler.AddParameter("Id", phone.Id);
                dbHandler.Command.ExecuteNonQuery();
                isSuccess = true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                isSuccess = false;
            }
            finally
            {
                dbHandler.Close();
            }
            return isSuccess;
        }


        public Boolean DeleteSubscription(Int32 id)
        {
            Boolean isSuccess = false;
            try
            {
                dbHandler.Open();
                dbHandler.AddCommand(@"DELETE FROM dbo.Phone
                                        WHERE id = @Id");
                dbHandler.AddParameter("Id", id);
                dbHandler.Command.ExecuteNonQuery();
                isSuccess = true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                isSuccess = false;
            }
            finally
            {
                dbHandler.Close();
            }
            return isSuccess;
        }*/
    }
}